package com.flyco.modules.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.util.DateUtils;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.pms.entity.PmsProduct;
import com.flyco.modules.pms.entity.PmsProductCategory;
import com.flyco.modules.pms.service.IPmsProductCategoryService;
import com.flyco.modules.pms.service.IPmsProductService;
import com.flyco.modules.sms.entity.SmsDiscount;
import com.flyco.modules.sms.entity.SmsDiscountDetail;
import com.flyco.modules.sms.entity.SmsDiscountProductCategoryRelation;
import com.flyco.modules.sms.entity.SmsDiscountProductRelation;
import com.flyco.modules.sms.mapper.SmsDiscountMapper;
import com.flyco.modules.sms.model.SmsDiscountModel;
import com.flyco.modules.sms.service.ISmsDiscountDetailService;
import com.flyco.modules.sms.service.ISmsDiscountProductCategoryRelationService;
import com.flyco.modules.sms.service.ISmsDiscountProductRelationService;
import com.flyco.modules.sms.service.ISmsDiscountService;
import net.sf.jsqlparser.statement.select.SetOperationList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 满减满折
 * @Author: flyco
 * @Date:   2020-05-06
 * @Version: V1.0
 */
@Service
public class SmsDiscountServiceImpl extends ServiceImpl<SmsDiscountMapper, SmsDiscount> implements ISmsDiscountService {

    @Autowired
    private IPmsProductCategoryService pmsProductCategoryService;
    @Autowired
    private IPmsProductService pmsProductService;
    @Autowired
    private ISmsDiscountProductCategoryRelationService smsDiscountProductCategoryRelationService;
    @Autowired
    private ISmsDiscountProductRelationService smsDiscountProductRelationService;
    @Autowired
    private ISmsDiscountDetailService smsDiscountDetailService;

    @Override
    public String checkDiscountModel(SmsDiscountModel smsDiscountModel) {
        if (oConvertUtils.isEmpty(smsDiscountModel)) return  "参数不可以为空";
        if (oConvertUtils.isEmpty(smsDiscountModel.getName())) return  "名称不可以为空";
        if (oConvertUtils.isEmpty(smsDiscountModel.getType())) return  "类型不可以为空";
        if (oConvertUtils.isEmpty(smsDiscountModel.getStartTime())) return  "开始时间不可以为空";
        if (oConvertUtils.isEmpty(smsDiscountModel.getEndTime())) return  "截止时间不可以为空";
        if (oConvertUtils.isEmpty(smsDiscountModel.getUseType())) return  "使用类型不可以为空";
        if (CollectionUtils.isEmpty(smsDiscountModel.getDetailList())) return "优惠内容不可以为空";
        if ( smsDiscountModel.getUseType() == 1) {
            //分类
            if (CollectionUtils.isEmpty(smsDiscountModel.getCategoryList())) return "关联分类不可以为空";
        } else if ( smsDiscountModel.getUseType() == 2) {
            //商品
            if (CollectionUtils.isEmpty(smsDiscountModel.getProductList())) return "关联商品不可以为空";
        }
        return null;
    }

    @Override
    public void formatDiscountModel(SmsDiscountModel smsDiscountModel) {
        //smsDiscountModel.setStartTime(DateUtils.beginOfDay(smsDiscountModel.getStartTime()));
        //smsDiscountModel.setEndTime(DateUtils.endOfDay(smsDiscountModel.getEndTime()));
        if (Arrays.asList(2, 4, 5).contains(smsDiscountModel.getType())) {
            BigDecimal a = new BigDecimal(1);
            BigDecimal b = new BigDecimal(10);
            for ( SmsDiscountDetail detail : smsDiscountModel.getDetailList()) {
                if ( detail.getDiscountValue().compareTo(a) > -1 ) {
                    detail.setDiscountValue(detail.getDiscountValue().divide(b));
                }
            }
        }

        if ( smsDiscountModel.getUseType() == 0) {
            //全场通用
            smsDiscountModel.getCategoryList().clear();
            smsDiscountModel.getProductList().clear();
        }
        else if ( smsDiscountModel.getUseType() == 1) {
            //分类
            smsDiscountModel.getProductList().clear();
        } else if ( smsDiscountModel.getUseType() == 2) {
            //商品
            smsDiscountModel.getCategoryList().clear();
        }
    }

    @Override
    public void addRelation(SmsDiscountModel smsDiscountModel) {
        if ( smsDiscountModel.getUseType() == 1) {
            //分类
            List<SmsDiscountProductCategoryRelation> categoryRelations = new ArrayList<>();
            Collection<PmsProductCategory> productCategories = pmsProductCategoryService.listByIds(smsDiscountModel.getCategoryList());
            for ( PmsProductCategory category : productCategories) {
                SmsDiscountProductCategoryRelation categoryRelation = new SmsDiscountProductCategoryRelation();
                categoryRelation.setDiscountId(smsDiscountModel.getId());
                categoryRelation.setProductCategoryId(category.getId());
                categoryRelation.setProductCategoryName(category.getName());
                categoryRelations.add(categoryRelation);
            }
            smsDiscountProductCategoryRelationService.saveBatch(categoryRelations);

        } else if ( smsDiscountModel.getUseType() == 2) {
            //商品
            List<SmsDiscountProductRelation> productRelations = new ArrayList<>();
            Collection<PmsProduct> products = pmsProductService.listByIds(smsDiscountModel.getProductList());
            for (PmsProduct product : products ) {
                SmsDiscountProductRelation productRelation = new SmsDiscountProductRelation();
                productRelation.setDiscountId(smsDiscountModel.getId());
                productRelation.setProductId(product.getId());
                productRelation.setProductName(product.getName());
                productRelation.setProductSn(product.getProductSn());
                productRelations.add(productRelation);
            }
            smsDiscountProductRelationService.saveBatch(productRelations);
        }
    }

    @Override
    public void editRelation(SmsDiscountModel smsDiscountModel) {
        //分类
        LambdaQueryWrapper<SmsDiscountProductCategoryRelation> categoryQuery = new LambdaQueryWrapper<>();
        categoryQuery.eq(SmsDiscountProductCategoryRelation::getDiscountId,smsDiscountModel.getId());
        smsDiscountProductCategoryRelationService.remove(categoryQuery);
        //商品
        LambdaQueryWrapper<SmsDiscountProductRelation> productQuery = new LambdaQueryWrapper<>();
        productQuery.eq(SmsDiscountProductRelation::getDiscountId,smsDiscountModel.getId());
        smsDiscountProductRelationService.remove(productQuery);

        addRelation(smsDiscountModel);
    }


    @Override
    public void addDetail(SmsDiscountModel smsDiscountModel) {
        List<SmsDiscountDetail> details = smsDiscountModel.getDetailList();
        for ( SmsDiscountDetail detail : details) {
            detail.setDiscountId(smsDiscountModel.getId());
        }
        smsDiscountDetailService.saveBatch(details);
    }

    @Override
    public void editDetail(SmsDiscountModel smsDiscountModel) {
        LambdaQueryWrapper<SmsDiscountDetail> detailQuery = new LambdaQueryWrapper<>();
        detailQuery.eq(SmsDiscountDetail::getDiscountId,smsDiscountModel.getId());
        smsDiscountDetailService.remove(detailQuery);
        addDetail(smsDiscountModel);
    }

}
